<?php

class Model_DbTable_Apos_Ktdata extends Zend_Db_Table_Abstract {
	


	
	public function isBeenTransfer($sCode,$location){
		$whereStr = "SCODE LIKE '".$sCode."' AND TO_LOC LIKE '".$location."' AND ADJ_QTY >0";
		$row = $this->fetchRow($whereStr);
		if(!$row) return false;
		return $row->toArray();
	
	}
	
	public function isFirstKt($sCode,$location,$ktNo){
		
		$whereStr = "SCODE LIKE '".$sCode."' AND TO_LOC LIKE '".$location."' ";
		$row = $this->fetchRow($whereStr,"TRAN_NO");
		$row = $row->toArray();
		//d($row);
		if(trim($row["TRAN_NO"]) != $ktNo){
			return false;
		}
		else{
			return true;
		}
	}
	public function listKT($ktNo,$location){
	
		$whereStr = "TRAN_NO LIKE '{$ktNo}' AND TO_LOC LIKE '{$location}'";
		$row = $this->fetchAll($whereStr);
		if(empty($row)) return false;
		return $row->toArray();
			
	}
	
	public function thisWeekTransfer($sCode){
			
		$totalQty = 0;
		$thisMonday = Model_DatetimeHelper::getThisWeekMonday();
		$whereStr = "SCODE LIKE '".$sCode."' AND TRAN_NO IN ( SELECT TRAN_NO FROM [APOS1108].[dbo].[TRANSFER] WHERE DATE >= '".$thisMonday." 00:00:00' AND STATUS = ''  ) ";
	
		$rows = $this->fetchAll($whereStr);
		if(!$rows) return 0;
		foreach ($rows as $row){
				
			$totalQty +=$row['QTY'];
		}
	
		return $totalQty;
	
	}
	
	public function sCodeExist($sCode,$kaNo,$location){
	
		$whereStr = "SCODE LIKE '{$sCode}' AND  TRAN_NO LIKE '".$kaNo."' AND TO_LOC LIKE '{$location}' ";
		$row = $this->fetchRow($whereStr);
		if(!$row) return false;
		return $row->toArray();
	
	
	}
	
	public function ktFrom($ktNo,$location){
		
		$whereStr = "TRAN_NO LIKE '{$ktNo}' AND TO_LOC LIKE '{$location}'";
		$row = $this->fetchRow($whereStr);
		//d()
		if(!$row){ return false;}
		if(trim($row['FR_LOC']) == "WH"){return true;}
		return false; 
	}
	
}

?>